<template>
  <div>
    <el-dialog title="修改用户信息" v-model="dialogVisible" width="40%">
      <el-form :model="form" label-width="100px">
        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :headers="headers"
            drag
          >
            <el-image style="width: 50px; height: 50px" v-if="form.avatar" :src="form.avatar" class="avatar"></el-image>
            <div v-else>
              <el-icon class="el-icon--upload"><upload-filled /></el-icon>
              <div class="el-upload__text">
                Drop file here or
                <em>click to upload</em>
              </div>
            </div>
          </el-upload>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择角色">
            <el-option label="管理员" value="admin"></el-option>
            <el-option label="普通用户" value="user"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">提交</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-row class="list-tab">
      <el-table :data="tableData" :show-overflow-tooltip="true" :header-cell-style="{ backgroundColor: '#f8f8f9' }">
        <el-table-column prop="id" label="序号" width="100"></el-table-column>
        <el-table-column prop="username" label="用户名" width="150"></el-table-column>
        <el-table-column prop="avatar" label="头像" width="200">
          <template #default="scope">
            <el-image
              :src="scope.row.avatar"
              style="width: 50px; height: 50px"
              :zoom-rate="1.2"
              :max-scale="7"
              :min-scale="0.2"
              :preview-src-list="[scope.row.avatar]"
              :initial-index="0"
              fit="cover"
              :hide-on-click-modal="true"
              :preview-teleported="true"
            />
          </template>
        </el-table-column>
        <el-table-column prop="role" label="角色" width="200"></el-table-column>
        <el-table-column prop="email" label="邮箱" width="200"></el-table-column>
        <el-table-column label="状态">
          <template #default="scope">
            <el-switch
              @click="handleChange(scope.row)"
              v-model="scope.row.status"
              active-value="1"
              inactive-value="0"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" width="200" align="center">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tab-page">
        <el-pagination
          v-model:current-page="searchParams.pageNum"
          v-model:page-size="searchParams.pageSize"
          :page-sizes="[5, 10, 20, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="totalItem"
          @size-change="getList"
          @current-change="getList"
        />
      </div>
    </el-row>
  </div>
</template>

<script setup>
  import { UploadFilled } from '@element-plus/icons-vue'
  import { ref, computed, onMounted } from 'vue'
  import { ElMessage } from 'element-plus'
  import { useUserStore } from '@/stores/user'
  import { getUserList, updateUser, deleteUser } from '@/api/system/sys_user'

  const store = useUserStore()
  const uploadUrl = ref(import.meta.env.VITE_APP_BASE_API + '/api/user/upload')
  const headers = computed(() => {
    return {
      Authorization: store.user.token
    }
  })
  const dialogVisible = ref(false)
  const form = ref({
    id: '',
    username: '',
    avatar: '',
    role: '',
    email: '',
    status: ''
  })
  const searchParams = ref({
    pageNum: 1,
    pageSize: 5
  })
  const totalItem = ref(0)
  const tableData = ref([])
  const getList = () => {
    getUserList(searchParams.value).then(res => {
      if (res.data) {
        tableData.value = res.data.content
        totalItem.value = res.data.totalSize
      } else {
        ElMessage({
          type: 'error',
          message: res.msg
        })
      }
    })
  }
  const handleChange = row => {
    updateUser(row).then(res => {
      getList()
    })
  }

  const handleEdit = (index, row) => {
    dialogVisible.value = true
    form.value = row
  }
  const handleDelete = (index, row) => {
    deleteUser(row).then(res => {
      getList()
    })
  }

  const handleSubmit = () => {
    updateUser(form.value).then(res => {
      getList()
    })
    dialogVisible.value = false
  }
  onMounted(() => {
    getList()
  })

  const handleAvatarSuccess = res => {
    form.value.avatar = res.url
  }
</script>

<style scoped></style>
